package com.xw.childhealth.controller;

import com.xw.childhealth.VO.DataDictionaryVO;
import com.xw.childhealth.VO.ResultVO;
import com.xw.childhealth.entity.DataDictionary;
import com.xw.childhealth.service.DataDictionaryService;
import com.xw.childhealth.util.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: y
 * @CreateDate: 2019-11-07 16:27
 * @Version: 1.0
 */
@RestController
@RequestMapping("/dataDictionary")
public class DataDictionaryController {

    @Autowired
    private DataDictionaryService dataDictionaryService;


    /**
     * 获取数据字典
     *
     * @return
     */
    @PostMapping("/getDataDictionary")
    public ResultVO getDataDictionary() {
        List<DataDictionaryVO> dataDictionaryVOList = new ArrayList<>();

        //查找下级节点
        List<DataDictionaryVO> list = recursionData(dataDictionaryVOList, 0);

        return ResultVOUtil.success(list);
    }

    /**
     * 查找下级节点目录
     *
     * @param dataDictionaryVOList 数据字典 List
     * @param id                   查找的上级 id
     * @return
     */
    private List<DataDictionaryVO> recursionData(List<DataDictionaryVO> dataDictionaryVOList, Integer id) {

        List<DataDictionary> parentNode = dataDictionaryService.findParentNode(id);
        if (parentNode == null || parentNode.isEmpty()) {
            return dataDictionaryVOList;
        }
        parentNode.forEach(e -> {
            DataDictionaryVO dataDictionaryVO = new DataDictionaryVO();
            dataDictionaryVO.setId(e.getId());
            dataDictionaryVO.setTitle(e.getInformation());

            if (e.getInformation() != null) {

                List<DataDictionaryVO> list = recursionData(new ArrayList<>(), e.getId());

                dataDictionaryVO.setChildren(list.size() == 0 ? null : list);

                dataDictionaryVOList.add(dataDictionaryVO);
            }

        });

        return dataDictionaryVOList;
    }

    /**
     * 查找节点下面的内容
     *
     * @param nodeId
     * @return
     */
    @PostMapping("/getNodeInfo/{nodeId}")
    public ResultVO getNodeInfo(@PathVariable Integer nodeId) {

        List<DataDictionary> dictionaries = dataDictionaryService.findParentNode(nodeId);

        return ResultVOUtil.success(dictionaries);
    }

    /**
     * 修改或者添加
     *
     * @param type
     * @param nodeId
     * @param code1
     * @param code2
     * @param value1
     * @param addRecordNode
     * @param map
     * @return
     */
    @RequestMapping("/data-upd")
    public ModelAndView edit(@RequestParam(value = "type", required = false) String type, @RequestParam(value = "nodeId", required = false) Integer nodeId,
                             @RequestParam(value = "code1", required = false) String code1, @RequestParam(value = "code2", required = false) String code2,
                             @RequestParam(value = "value1", required = false) String value1,
                             @RequestParam(value = "addRecordNode", required = false) Integer addRecordNode, Map<String, Object> map) {
        if (type == null) {
            return new ModelAndView("/pages/admin/data-dictionary", map);
        }
        if (type.equals("toUpd")) {
            DataDictionary one = dataDictionaryService.findOneById(nodeId);
            map.put("type", type);
            map.put("result", one);
        } else if (type.equals("upd")) {
            DataDictionary one = dataDictionaryService.findOneById(nodeId);
            one.setCode(code1);
            one.setAssistCode(code2);
            one.setValue(value1);
            dataDictionaryService.save(one);
            return new ModelAndView("/pages/admin/data-dictionary", map);
        }
        if (type.equals("toAdd")) {
            map.put("addRecordNode", addRecordNode);
        } else if (type.equals("add")) {
            dataDictionaryService.save(new DataDictionary().setCode(code1).setAssistCode(code2).setValue(value1).setParentId(addRecordNode).setCreateTime(new Date()).setUpdateTime(new Date()));
            return new ModelAndView("/pages/admin/data-dictionary", map);
        }
        return new ModelAndView("/pages/admin/data-add", map);
    }

    @RequestMapping("/data-del")
    public ModelAndView del(@RequestParam("id") Integer id) {
        DataDictionary result = dataDictionaryService.findOneById(id);
        if (result != null) {
            dataDictionaryService.delRecord(id);
        }
        return new ModelAndView("/pages/admin/data-dictionary");
    }

}
